package com.fbinggun.fbinggunutil.fgive.command.args;

import com.fbinggun.fbinggunutil.FBinggunUtil;
import com.fbinggun.fbinggunutil.command.ACommand;
import com.fbinggun.fbinggunutil.command.MainCommand;
import com.fbinggun.fbinggunutil.gui.FHolder;
import com.fbinggun.fbinggunutil.gui.Gui;
import com.fbinggun.fbinggunutil.gui.SlotItem;
import com.fbinggun.fbinggunutil.gui.item.PgDn;
import com.fbinggun.fbinggunutil.gui.item.PgUp;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ItemList extends ACommand {
    public ItemList(MainCommand mainCommand) {
        super(mainCommand);
    }

    @Override
    public String getPresentation() {
        return "&e/fgive list 列出所有物品";
    }

    @Override
    public boolean isOp() {
        return true;
    }

    @Override
    public String getPermission() {
        return "";
    }

    @Override
    public String getId() {
        return "list";
    }

    @Override
    public boolean run(CommandSender sender, String[] args) {
                FgiveListGui gui= new FgiveListGui(getGuiConfig());
                gui.openInventory((Player) sender,1);
       return false;
    }

    @Override
    public boolean isPlayer() {
        return true;
    }

    public ConfigurationSection getGuiConfig(){
        YamlConfiguration yamlConfiguration = new YamlConfiguration();
        yamlConfiguration.set("Gui.Title","&a物品仓库");
        yamlConfiguration.set("Gui.Size",54);
        ConfigurationSection   config1 =  yamlConfiguration.getConfigurationSection("Gui");
        return config1;
    }
}
class FgiveListGui extends Gui {
    private static final int ITEMS_PER_PAGE = 9 * 5;
    public FgiveListGui(ConfigurationSection config) {
        super(config);
        setCancelPlayerInvCancel(false);
    }

    @Override
    public void openInventory(Player player, int page) {
        if(!isOpen(player)) {
            return;
        }
        if(!isPageExists(page)) {
            return;
        }
        FHolder fHolder = new FHolder(this, page,player);
        Inventory inventory = fHolder.getInventory();
        Map<Integer, ItemStack> items = getPageItem(page);
        int x=0;
        for (ItemStack item : items.values()) {
           inventory.setItem(x, item);
           x++;
        }
        if(page != 1) {
            FgivePgUp pgUp= new FgivePgUp("1",null);
            fHolder.putTemp(45,pgUp);
            inventory.setItem(45,getPrevious());
        }
        if(isPageExists(page+1)) {
            FgivePgDn pgDn= new FgivePgDn("1",null);
            fHolder.putTemp(53,pgDn);
            inventory.setItem(53,getNext());
        }
        player.openInventory(inventory);
    }

    // 获取总页数
    public int getTotalPages() {
        Map<String, ItemStack> allItem = FBinggunUtil.plugin.getFgiveAPI().getItemStackMap();
        int totalItems = allItem.size();
        return (totalItems + ITEMS_PER_PAGE + 1) / ITEMS_PER_PAGE; // 向上取整
    }

    // 判断页数是否存在
    public boolean isPageExists(int page) {
        return page > 0 && page <= getTotalPages();
    }

    public Map<Integer, ItemStack> getPageItem(int page) {
        Map<String, ItemStack> allItem = FBinggunUtil.plugin.getFgiveAPI().getItemStackMap();
        Map<Integer, ItemStack> pageItems = new HashMap<>();
        // 计算起始索引和结束索引
        int startIndex = (page - 1) * ITEMS_PER_PAGE;
        int endIndex = Math.min(startIndex + ITEMS_PER_PAGE, allItem.size());

        // 使用迭代器遍历 itemMap，按索引范围提取物品
        Iterator<Map.Entry<String, ItemStack>> iterator = allItem.entrySet().iterator();
        int currentIndex = 0;
        while (iterator.hasNext() && currentIndex < endIndex) {
            Map.Entry<String, ItemStack> entry = iterator.next();
            if (currentIndex >= startIndex) {
                pageItems.put(currentIndex - startIndex, entry.getValue());
            }
            currentIndex++;
        }
        return pageItems;
    }



    @Override
    public boolean isOpen(Player player) {
        Map<String, ItemStack> allItem = FBinggunUtil.plugin.getFgiveAPI().getItemStackMap();
        if(allItem.isEmpty()){
            FBinggunUtil.plugin.sendPlayerMsg(player,"&c物品库为空");
            return false;
        }
        return true;
    }

    @Override
    public void run(InventoryClickEvent inventoryClickEvent, FHolder fHolder) {
            Player player = (Player) inventoryClickEvent.getWhoClicked();
            inventoryClickEvent.setCancelled(false);
            SlotItem slotItem= fHolder.getTempMap(inventoryClickEvent.getSlot());
            if(slotItem==null){
                return;
            }
              slotItem.run(player,inventoryClickEvent,FBinggunUtil.plugin,fHolder);
    }
    //下一页 物品
    public ItemStack getNext(){
        ItemStack item= new ItemStack(Material.ARROW);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName("§a下一页");
        item.setItemMeta(meta);
    return item;
    }
    //上一页 物品
    public ItemStack getPrevious(){
        ItemStack item= new ItemStack(Material.ARROW);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName("§a上一页");
        item.setItemMeta(meta);
        return item;
    }


}

class FgivePgUp extends PgUp{


    public FgivePgUp(String id, ConfigurationSection config) {
        super(id, config);
    }
    public ItemStack getPrevious(){
        ItemStack item= new ItemStack(Material.ARROW);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName("§a上一页");
        item.setItemMeta(meta);
        return item;
    }
}
class FgivePgDn extends PgDn {


    public FgivePgDn(String id, ConfigurationSection config) {
        super(id, config);
    }
    public ItemStack getNext(){
        ItemStack item= new ItemStack(Material.ARROW);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName("§a下一页");
        item.setItemMeta(meta);
        return item;
    }
}